package com.cykj.handler;

import com.cykj.entity.GoodsEntity;
import com.cykj.service.ProDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class proDetailHandler {

    @Autowired
    private ProDetailService proDetailService;

    @RequestMapping("/Detail")
    public Map<String,Object> detail(HttpServletRequest req) {
        Map<String ,Object> map = new HashMap<>();
        String id = req.getParameter("id");
        List<GoodsEntity> list = proDetailService.selGoodsId(Integer.parseInt(id));

        map.put("data",list);
        return map;
    }

    @RequestMapping("/addorder")
    public Map<String,Object> addorder(HttpServletRequest req){
        Map<String ,Object> map = new HashMap<>();
        String goodsid = req.getParameter("id");
        String number = req.getParameter("number");

        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String time =sbf.format(d);


         String state = "未付款";

        int i = proDetailService.addorder(Integer.parseInt(goodsid),time, Integer.parseInt(number),state);

        if (i>0){
            map.put("succ",1);
        }else {
            map.put("succ",0);
        }

        return map;
    }
}
